package com.sc3.game.dataaccess;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;

import com.sc3.game.serverbean.UserBean;

public class UserDA {

	public static UserBean readUserBean(ResultSet result) throws SQLException {
		UserBean user = new UserBean();
		user.setID(result.getInt("id"));
		user.setEmail(result.getString("email"));
		user.setPassword(result.getString("password"));
		user.setTime(result.getDate("time"));
		return user;
	}

	public static boolean writeUserBean(Connection connection, UserBean user)
			throws SQLException {
		PreparedStatement statement = connection
				.prepareStatement("INSERT INTO user SET id=?,email=?,password=?,time=?,name=?");
		setUserBean(user, statement);
		int exeLine = statement.executeUpdate();
		return exeLine > 0;
	}

	private static void setUserBean(UserBean user, PreparedStatement statement)
			throws SQLException {
		statement.setInt(1, user.getID());
		statement.setString(2, user.getEmail());
		statement.setString(3, user.getPassword());
		statement.setString(4, DAUtil.date2LongString(user.getTime()));
		statement.setString(5, user.getName());
	}

	public static boolean updateUserBean(Connection connection, UserBean user)
			throws SQLException {
		PreparedStatement statement = connection
				.prepareStatement("UPDATE user SET email=?,password=?,time=?,name=?");
		setUserBeanNoID(user, statement);
		int exeLine = statement.executeUpdate();
		statement.close();
		return exeLine > 0;
	}

	private static void setUserBeanNoID(UserBean user,
			PreparedStatement statement) throws SQLException {
		statement.setString(1, user.getEmail());
		statement.setString(2, user.getPassword());
		String time = DAUtil.date2LongString(user.getTime());
		statement.setString(3, time);
		statement.setString(4, user.getName());
	}
	
	public static UserBean register(Connection connection, String email,String name,String password) throws SQLException{
		UserBean user = new UserBean();
		user.setEmail(email);
		user.setName(name);
		user.setPassword(password);
		user.setTime(new Date(System.currentTimeMillis()));
		int id = addUserBean(connection, user);
		user.setID(id);
		return user;
	}

	public static int addUserBean(Connection connection, UserBean user)
			throws SQLException {
		int id = 0;
		PreparedStatement statement = connection.prepareStatement(
				"INSERT INTO user SET email=?,password=?,time=?,name=?",
				Statement.RETURN_GENERATED_KEYS);
		setUserBeanNoID(user, statement);
		int exeLine = statement.executeUpdate();
		if (exeLine > 0) {
			ResultSet result = statement.getGeneratedKeys();
			result.next();
			id = result.getInt(1);
			result.close();
		}
		statement.close();
		return id;
	}

	public static UserBean login(Connection connection, String email,
			String password) throws SQLException {
		UserBean user = null;
		PreparedStatement statement = connection.prepareStatement(
				"SELECT * FROM user WHERE email=?,password=?",
				Statement.RETURN_GENERATED_KEYS);
		statement.setString(1, email);
		statement.setString(2, password);
		ResultSet result = statement.executeQuery();
		if (result.next()) {
			user = readUserBean(result);
		}
		result.close();
		statement.close();
		return user;
	}

}
